const express = require('express');
const router = express.Router();
const articleHandle = require('../router_handle/article_handle.js');
// 处理文件上传
const multer = require('multer');
const path = require('path');

const storage = multer.diskStorage({
	destination: function (req, file, cb) {
		cb(null, path.join(__dirname, '../uploads'))
	},
	filename: function (req, file, cb) {
		const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9)
		cb(null, file.fieldname + '-' + uniqueSuffix + path.extname(file.originalname))
	}
})
const upload = multer({
	storage: storage
})

router.get('/total', articleHandle.getArticleTotal);
router.post('/add', upload.single('cover_img'), articleHandle.addArticle);
router.get('/list', articleHandle.getArticleList);
router.get('/delete/:id', articleHandle.deleteArticleById);
router.get('/get/:id', articleHandle.getArticleById);
router.post('/edit', upload.single('cover_img'), articleHandle.updateArticle);

module.exports = {
	router
}